package com.service.bank.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.client.model.pojo.bank.ArBankcard;
import com.client.model.pojo.bank.ArCardtype;
import com.client.model.pojo.bank.ArElectronic;
import com.client.model.pojo.bank.TElectronicCard;
import com.client.model.pojo.financial.AiruiFinancialUser;
import com.client.model.pojo.user.ArUser;
import com.client.model.vo.common.Result;
import com.client.model.vo.common.ResultCodeEnum;
import com.client.model.vo.creditLoanVo.ArBanksVo;
import com.service.bank.feign.FinancialUserFeginService;
import com.service.bank.mapper.ArCardtypeMapper;
import com.client.model.vo.deposit.BankVo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.service.bank.mapper.ArCardtypeMapper;
import com.service.bank.mapper.TElectronicCardMapper;
import com.service.bank.service.ArBankcardService;
import com.service.bank.mapper.ArBankcardMapper;
import com.service.bank.service.ArElectronicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liuwenju
 * @description 针对表【ar_bankcard】的数据库操作Service实现
 * @createDate 2024-04-01 20:55:27
 */
@Slf4j
@Service
public class ArBankcardServiceImpl extends ServiceImpl<ArBankcardMapper, ArBankcard>
        implements ArBankcardService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private ArCardtypeMapper arCardtypeMapper;

    @Autowired
    private ArBankcardMapper arBankcardMapper;
    @Autowired
    private ArElectronicService arElectronicService;


    @Autowired
    private FinancialUserFeginService financialUserFeginService;

    @Autowired
    private TElectronicCardMapper tElectronicCardMapper;

    @Override
    public Result findBank(String token) {
        ArUser arUser = JSON.parseObject(redisTemplate.opsForValue().get("user:login" + token), ArUser.class);
        List<BankVo> bankVos = arBankcardMapper.selectJoinList(BankVo.class,
                new MPJLambdaWrapper<ArBankcard>()
                        .selectAll(ArBankcard.class)
                        .selectAll(ArCardtype.class)
                        .innerJoin(ArCardtype.class, ArCardtype::getTid, ArBankcard::getTid)
                        .eq("uid", arUser.getUid()));
        if (bankVos == null) {
            return Result.build(null, 500, "尚未绑定银行卡");
        }
        return Result.build(bankVos, ResultCodeEnum.SUCCESS);
    }

    @Override
    public ArCardtype getBankCardByCid(Long cid) {
        return this.arCardtypeMapper.getBankCardByCid(cid);
    }



    @Override
    public Result savePrice(String bankCard, BigDecimal price,String token) {

        ArBankcard arBankcard = arBankcardMapper.selectOne(new QueryWrapper<ArBankcard>()
                .eq("bank_card", bankCard));
        if (arBankcard == null) {
            return Result.build(null, 500, "银行卡不存在");
        }
        //将电子账户进行添加
        String s = redisTemplate.opsForValue().get("user:login" + token);
        ArUser arUser = JSON.parseObject(s, ArUser.class);
        ArElectronic uid = arElectronicService.getOne(new QueryWrapper<ArElectronic>()
                .eq("uid", arUser.getUid()));
        if (uid == null) {
            return Result.build(null, 500, "电子账户不存在");
        }
        uid.setBalance(uid.getBalance().add(price));
        arElectronicService.updateById(uid);
        arBankcard.setBalance(arBankcard.getBalance().subtract(price));
        arBankcardMapper.updateById(arBankcard);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result<ArBankcard> pwdIsRight(String password) {
        QueryWrapper<ArBankcard> arBankcardQueryWrapper = new QueryWrapper<>();
        //加密用户传进来的密码
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        arBankcardQueryWrapper.eq("pay_password",s);
        ArBankcard arBankcard = this.arBankcardMapper.selectOne(arBankcardQueryWrapper);
        if (ObjectUtil.isEmpty(arBankcard)) {
            return Result.build(null, 500, "密码错误");
        }
        return Result.build(arBankcard, ResultCodeEnum.SUCCESS);
    }

    @Override
    public ArBankcard getCreditCardByCardNum(String cardNum,String password) {
        log.info("cardNum:{},password:{}",cardNum,password);
        QueryWrapper<ArBankcard> arBankcardQueryWrapper = new QueryWrapper<>();
        arBankcardQueryWrapper.eq("bank_card",cardNum);
        ArBankcard arBankcard = this.arBankcardMapper.selectOne(arBankcardQueryWrapper);
        log.info("当前的银行卡是-------->{}",arBankcard);
        if (ObjectUtil.isEmpty(arBankcard)){
            return null;
        }
        String pwd=password+arBankcard.getSalt();
        String s = DigestUtils.md5DigestAsHex(pwd.getBytes());
        System.out.println(s);
        arBankcardQueryWrapper.eq("pay_password",s);
        return this.arBankcardMapper.selectOne(arBankcardQueryWrapper);
    }

    @Override
    public ArBankcard getBankCardByCardNum(String cardNum) {
        log.info("cardNum------->{}",cardNum);
        QueryWrapper<ArBankcard> arBankcardQueryWrapper = new QueryWrapper<>();
        arBankcardQueryWrapper.eq("bank_card",cardNum);
        return this.arBankcardMapper.selectOne(arBankcardQueryWrapper);
    }

    @Override
    public Result payFin(String bankCard, BigDecimal price, String token,Long id,Integer remark) {
        String s = redisTemplate.opsForValue().get("user:login" + token);
        ArUser arUser = JSON.parseObject(s, ArUser.class);
        if(bankCard ==null){
            //说明用户使用的电子账户
            ArElectronic uid = arElectronicService.getOne(new QueryWrapper<ArElectronic>()
                    .eq("uid", arUser.getUid()));
            if(uid == null){
                return Result.build(null,500,"未注册电子账户");
            }
            if(uid.getBalance().compareTo(price) < 0){
                return Result.build(null,500,"余额不足");
            }
            uid.setBalance(uid.getBalance().subtract(price));
            System.err.println(uid);
            arElectronicService.updateById(uid);
            //理财购买记录表
            AiruiFinancialUser airuiFinancialUser = new AiruiFinancialUser();
            airuiFinancialUser.setStatus(0);
            airuiFinancialUser.setUid(arUser.getUid());
            airuiFinancialUser.setRemark(remark);
            airuiFinancialUser.setItemId(id);
            financialUserFeginService.save(airuiFinancialUser);
            return Result.build(null,200,"购买成功");
        }
        ArBankcard bank = arBankcardMapper.selectOne(new QueryWrapper<ArBankcard>().eq("bank_card", bankCard));
        if(bank == null){
            return Result.build(null,500,"银行卡不存在");
        }
        if(bank.getBalance().compareTo(price) < 0){
            return Result.build(null,500,"余额不足");
        }
        bank.setBalance(bank.getBalance().subtract(price));
        arBankcardMapper.updateById(bank);
        AiruiFinancialUser airuiFinancialUser = new AiruiFinancialUser();
        airuiFinancialUser.setStatus(0);
        airuiFinancialUser.setUid(arUser.getUid());
        airuiFinancialUser.setRemark(remark);
        airuiFinancialUser.setItemId(id);
        financialUserFeginService.save(airuiFinancialUser);
        return Result.build(null,200,"购买成功");
    }

    @Override
    public List<ArBanksVo> getBankCardList(String token) {
        String userInfo = redisTemplate.opsForValue().get("user:login" + token);
        ArUser sysUser = JSON.parseObject(userInfo, ArUser.class);
        Long uid = sysUser.getUid();
        System.out.println(uid);
        ArElectronic arElectronic = this.arElectronicService.getElectronicByEid(uid);
        System.err.println("你好："+arElectronic);
        List<ArBanksVo> tElectronicCards = this.tElectronicCardMapper.getElectronicCardByEid(arElectronic.getEid());
        tElectronicCards.forEach(System.err::println);
        return tElectronicCards;
    }


}




